/**
 * 				ClientTimerThread.java
 *				------------
 *		
 *   Author:	bin04017@gmail.com
 *	 Created:	13.12.2006
 *	 Project:	MP3-Sync (Mochito)
 *	 Package:	mochito.clientClasses.clientTimer
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */
package mochito.clientClasses.clientTimer;

import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import mochito.sound.SoundPlayer;

/**
 * @author bin04017
 * 
 */
public class ClientTimerThread extends Thread {
	
	public boolean running = false;

	private Set<ClientTimerThreadItem> items = new HashSet<ClientTimerThreadItem>();

	/**
	 * Add an alarm to our list of monitored alarms
	 */
	public void addClientTimerThreadItem(ClientTimerThreadItem item) {
		this.items.add(item);
	}

	/**
	 * Remove an alarm from our list of monitored alarms
	 */
	public void removeClientTimerThreadItem(ClientTimerThreadItem item) {
		this.items.remove(item);
	}

	public void run() {
		this.running = true;
		Calendar now = Calendar.getInstance();
		while (running) {
			try {
				now.setTimeInMillis(System.currentTimeMillis());
				for (Iterator i = items.iterator(); i.hasNext();) {
					ClientTimerThreadItem item = (ClientTimerThreadItem) i.next();
					if (!item.isFired() && item.checkAlarm(now)) {
						SoundPlayer soundPlayer = new SoundPlayer(item.getMusicItem());
						soundPlayer.play();
						//Runtime.getRuntime().exec(item.getCommand());
					}
				}

				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
